<template>
  <el-tabs type="border-card">
    <el-tab-pane label="微信基础设置" class="swiper-container">
      <el-form ref="refForm" :model="model.form"
        label-width="220px"
        autocomplete="off">
        <el-form-item v-for="(item, index) in model.form" :label="item.title">
          <!-- input -->
          <el-input v-if="item.type==='text'" v-model.trim="item.value" :placeholder="item.title" :style="{
            width: '460px',
          }"/>

          <!-- image -->
          <div v-if="item.type === 'image'">
            <el-upload class="upload-demo" ref="refUpload"
              accept="jpg,jpeg,png"
              :action="uploadServer"
              :headers="{ Authorization: local.get('ADMIN.TOKEN') || '' }"
              :on-success="(response, file) => uploadSuccess(response, file, index)"
              :on-error="uploadError"
              :show-file-list="false">
              <img v-if="item.value" :src="item.value"
                class="avatar"
                style="width: 120px; height: 120px; border: 1px solid #e9e9e9;"
                alt="item.value">
              <el-icon v-else class="el-icon-plus avatar-uploader-icon">
                <upload-filled/>
              </el-icon>
              <template #tip>
                <div class="el-upload__tip" v-html="item.tips"></div>
              </template>
            </el-upload>
          </div>

          <!-- select -->
          <div v-if="item.type === 'select'">
            <el-select v-model="item.value" :style="{ width: '460px' }">
              <el-option
                v-for="item in item.options"
                :key="item.value"
                :label="item.value +' - ' +item.label"
                :value="item.value"
              />
            </el-select>
          </div>
        </el-form-item>
        <el-button type="primary" @click="submit">保存</el-button>
      </el-form>
    </el-tab-pane>
    <el-tab-pane label="上传支付证书" class="swiper-container">
      <cert-pem/>
    </el-tab-pane>
  </el-tabs>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import { local } from "../../utils/storage";
import { ElMessage } from "element-plus";
import { AdminConfigGet, AdminConfigPost } from "../../api/server/config";
import { uploadServer } from "../../utils";
import CertPem from './cert.vue';
import { UploadFilled } from '@element-plus/icons-vue'


const refForm = ref(null)
const model = reactive({
  form: null
})

onMounted(() => {
  getConfigInfo()
})

const getConfigInfo = () => {
  AdminConfigGet({ group: 'wechat' }).then(({ data: { info } }) => {
    console.info(info)
    model.form = info
  })
}

const submit = () => {
  AdminConfigPost(model.form).then(() => {
    ElMessage.success('配置信息已更新')
  })
}


// 文件上传成功回调
const uploadSuccess = (response, file, index) => {
  if (response.code === 200) {
    model.form[index].value = response.data
  } else {
    ElMessage.error('Upload failed: ' + response.msg)
  }
}
// 文件上传失败回调
const uploadError = (e, file) => {
  ElMessage.error('Upload failed: ' + e.msg)
}
</script>

<style scoped>
.container {
  padding: 0 20px;
}

.header span {
  color: rgb(82, 171, 173);
}

.el-button--primary {
  margin-left: 180px;
}

.avatar {
  border-radius: 10px;
}

.avatar-uploader-icon {
  width: 120px;
  height: 120px;
  border-radius: 10px;
  border: 1px solid var(--el-border-color-light);
  font-size: 36px;
  color: var(--el-color-info-light-5);
}
</style>
